package com.woniuxy.house.web;

import com.aliyun.oss.model.WebServiceRequest;
import com.woniuxy.house.entity.*;
import com.woniuxy.house.service.CommentService;
import com.woniuxy.house.service.PostService;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.PostVo;
import com.woniuxy.house.vo.ResponseEntity;
import com.woniuxy.house.vo.ZhaoZuPostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api/post")
public class PostController {
    @Autowired
    private PostService postService;
@Autowired
private JwtUtil jwtUtil;
@Autowired
private CommentService commentService;
    @RequestMapping("getPage")
/**@魏锦鹏
 * 点击租友求租按钮进入租友求组界面显示数据
 * 按条件查询租友求租数据
 */
    public ResponseEntity getAll(@RequestBody PostVo postVo) {
        PageBean<Post> pageData = postService.getByPage(postVo.getPage() == null ? 1 : postVo.getPage());
        return new ResponseEntity<>("200", "success", pageData);
    }


    /**
     *
     */
    @RequestMapping("getPageBySearchText")
    public ResponseEntity getPageBySearchText(String searchText, Integer page) {
        if(searchText.equals("无")||searchText.equals("")){
            PageBean<Post> pageData=postService.getPageBySearchText(null,page);
            return new ResponseEntity<>("200", "success", pageData);
        }
PageBean<Post> pageData=postService.getPageBySearchText(searchText,page);
        return new ResponseEntity<>("200", "success", pageData);
    }
    @RequestMapping("addQiuZuPost")
    public ResponseEntity addQiuZuPost(@RequestBody PostVo post, HttpServletRequest request){
      String jwt= request.getHeader("jwt");
int id=(int)jwtUtil.verifyJWTGetUserId(jwt,"id");
        User user=new User();
        user.setId(id);
        Post post1=new Post();
        post1.setUser(user);
        post1.setTitle(post.getTitle());
        post1.setLatitude(post.getLatitude());
        post1.setLongitude(post.getLongitude());
        PostDetail postDetail=new PostDetail();
        postDetail.setContent(post.getContent());
        postDetail.setArea(post.getArea());
        postService.addQiuZuPost(post1,postDetail);

        return ResponseEntity.SUCCESS;
    }
    @RequestMapping("addZhaoZuPost")
    public ResponseEntity addZhaoZuPost(@RequestBody ZhaoZuPostVo zhaoZuPostVo, HttpServletRequest request){
        String jwt= request.getHeader("jwt");
        int id=(int)jwtUtil.verifyJWTGetUserId(jwt,"id");
        User user=new User();
        user.setId(id);
       zhaoZuPostVo.getPost().setUser(user);
       String houseNeed=zhaoZuPostVo.getConfig().toString();
       zhaoZuPostVo.getPostDetail().setHouseNeed(houseNeed);
        postService.addZhaoZuPost(zhaoZuPostVo.getPost(),zhaoZuPostVo.getPostDetail(),zhaoZuPostVo.getList());


        return ResponseEntity.SUCCESS;
    }
    @RequestMapping("getPostCommentByPage")
    public ResponseEntity getPostCommentByPage(Integer postId,Integer page){

return new ResponseEntity<>("200","ok",commentService.getByPostId(postId,page));

    }
    @RequestMapping("addPostComment")
    public ResponseEntity addPostComment(@RequestBody Comment comment){

        commentService.addComment(comment);
        return ResponseEntity.SUCCESS;
    }

    @RequestMapping("/getByUserId")
  public  ResponseEntity getByUserId(Integer uid,Integer page){
        return new ResponseEntity<>("200","ok",postService.getByUserIdPost(uid,page));
  }
}
